package com.techm.smptest.network;

import java.io.IOException;
import java.io.StringReader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class Response {
	private int eventType;
	private static final String delim = "|";
	private XmlPullParser xpp;
	private String response = "";

	public Response(String xml) {

		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			xpp = factory.newPullParser();

			xpp.setInput(new StringReader(xml));
			eventType = xpp.getEventType();

		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}

	}

	public String getPreLoginResponse() {

		try {
			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_TAG) {

					if (xpp.getName().equals("UMPERRORMSG")) {
						xpp.next();
						return "UMPERRORMSG|" + xpp.getText();
					} else if (xpp.getName().equals("FIRSTNAME")
							|| xpp.getName().equals("LASTNAME")) {
						xpp.next();
						response += xpp.getText() + delim;
					}
				}
				eventType = xpp.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return response;
	}

	public String getPRList() {

		try {
			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_TAG) {

					if (xpp.getName().equals("UMPERRORMSG")) {
						xpp.next();
						return "UMPERRORMSG|" + xpp.getText();
					} else if (xpp.getName().equals("PR_NUMBER")
							|| xpp.getName().equals("ITEM_NO")
							|| xpp.getName().equals("MATERIAL")
							|| xpp.getName().equals("SHORT_TEXT")
							|| xpp.getName().equals("QUANTITY")
							|| xpp.getName().equals("UNIT")
							|| xpp.getName().equals("UNIT_PRICE")
							|| xpp.getName().equals("TOTAL_PRICE")) {
						xpp.next();
						response += xpp.getText() + delim;
					}
				}
				eventType = xpp.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return response;
	}

	public String getPRDetails() {

		try {
			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_TAG) {

					if (xpp.getName().equals("UMPERRORMSG")) {
						xpp.next();
						return "UMPERRORMSG|" + xpp.getText();
					} else if (xpp.getName().equals("PR_NUMBER")
							|| xpp.getName().equals("ITEM_NO")
							|| xpp.getName().equals("MATERIAL")
							|| xpp.getName().equals("MAT_DESCRIPTION")
							|| xpp.getName().equals("QUANTITY")
							|| xpp.getName().equals("UNIT")
							|| xpp.getName().equals("UNIT_PRICE")) {
						xpp.next();
						response += xpp.getText() + delim;
					}
				}
				eventType = xpp.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return response;
	}

	public String getPRRelease() {

		try {
			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_TAG) {

					if (xpp.getName().equals("UMPERRORMSG")) {
						xpp.next();
						return "UMPERRORMSG|" + xpp.getText();
					} else if (xpp.getName().equals("STATUS")) {
						xpp.next();
						response += xpp.getText() + delim;
					}
				}
				eventType = xpp.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return response;
	}

	public String getPOList() {

		try {
			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_TAG) {

					if (xpp.getName().equals("UMPERRORMSG")) {
						xpp.next();
						return "UMPERRORMSG|" + xpp.getText();
					} else if (xpp.getName().equals("PO_NUMBER")
							|| xpp.getName().equals("PO_ITEM")
							|| xpp.getName().equals("PUR_MAT")
							|| xpp.getName().equals("SHORT_TEXT")
							|| xpp.getName().equals("DISP_QUAN")
							|| xpp.getName().equals("UNIT")
							|| xpp.getName().equals("NET_PRICE")
							|| xpp.getName().equals("TARGET_VAL")) {
						xpp.next();
						response += xpp.getText() + delim;
					}
				}
				eventType = xpp.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return response;
	}

	public String getPODetails() {

		try {
			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_TAG) {

					if (xpp.getName().equals("UMPERRORMSG")) {
						xpp.next();
						return "UMPERRORMSG|" + xpp.getText();
					} else if (xpp.getName().equals("PO_NUMBER")
							|| xpp.getName().equals("MATERIAL_NO")
							|| xpp.getName().equals("MAT_DESCRIPTION")
							|| xpp.getName().equals("QUANTITY")
							|| xpp.getName().equals("UNIT")
							|| xpp.getName().equals("UNIT_PRICE")) {
						xpp.next();
						response += xpp.getText() + delim;
					}
				}
				eventType = xpp.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return response;
	}

	public String getPORelease() {

		try {
			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_TAG) {

					if (xpp.getName().equals("UMPERRORMSG")) {
						xpp.next();
						return "UMPERRORMSG|" + xpp.getText();
					} else if (xpp.getName().equals("STATUS")) {
						xpp.next();
						response += xpp.getText() + delim;
					}
				}
				eventType = xpp.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return response;
	}

}
